package com.ereport.util;



import java.io.File;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;



public class SendMail{

	public static final String host="smtp.gmail.com";
	public static final String port ="465";
	/*public static final String email="info@kkcircuits.us";
	public static final String pass="choudary";*/
	
	public String sendMail( String frmemail, String frmpwd,String text, String subj,String toemail){
		String msg="";
		try{
			msg=sendMail(frmemail,frmpwd,host,port, text, subj, toemail);
		}catch(Exception e){
			e.printStackTrace();
		}
		return msg;
	}
	
	
	public static String sendMail(String d_email,String pass, String d_host, String d_port, String m_text, String m_subject,String m_to){
		Properties props = new Properties();
		props.put("mail.smtp.user", d_email);
		props.put("mail.smtp.host", d_host);
		props.put("mail.smtp.port", d_port);
		props.put("mail.smtp.starttls.enable","true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.socketFactory.port", d_port);
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");



		try
		{
			Authenticator auth = new SMTPAuthenticator(d_email,pass);
			Session session = Session.getInstance(props, auth);
			
			//System.out.println("subject is:"+m_subject+"\n Body Is :"+m_text+"\n File Name Is :"+fileName);
			// Define message
		    MimeMessage message =new MimeMessage(session);
		    message.setFrom(new InternetAddress(d_email));
		    message.setHeader("Content-Type" ,"text/plain" );
		    message.addRecipient(Message.RecipientType.TO,new InternetAddress(m_to));
		    message.setSubject(m_subject);

		    // create the message part 
		    MimeBodyPart messageBodyPart = new MimeBodyPart();

		    //fill message
		    messageBodyPart.setContent(m_text,"text/plain");

		    Multipart multipart = new MimeMultipart();
		    multipart.addBodyPart(messageBodyPart);

			// Put parts in message
		    message.setContent(multipart);

		    // Send the message
		    
		    Transport.send( message );
		   
			return "<br/>Successfully send Password to Your Email <br/><br/> Please Check Your Email for Password";
		}catch(MessagingException e){
			e.printStackTrace();
			return "Messsaging Exception";
		}catch (Exception mex){
			
			mex.printStackTrace();
			return "Invalid Email Address";
			}
		}
}
	
	
